package com.ysynzehao.controller;

import com.github.pagehelper.PageInfo;
import com.ysynzehao.model.User;
import com.ysynzehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * niuzehao
 * 2018/11/30 10:55.
 */
@RestController
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("list")
    public ResponseEntity<List<User>> list() {
        List<User> allUser = userService.getAllUser();
        return ResponseEntity.ok(allUser);
    }

    @GetMapping("listPage")
    public ResponseEntity<PageInfo<User>> listPage(int pageNum, int pageSize) {
        PageInfo<User> allUser = userService.getUserPage(pageNum, pageSize);
        return ResponseEntity.ok(allUser);
    }

    @GetMapping("getUser/{userId}")
    public ResponseEntity<User> getUser(@PathVariable("userId") Integer userId) {
        User user = userService.getUser(userId);
        return ResponseEntity.ok(user);
    }

    @PostMapping("saveUser")
    public ResponseEntity<Integer> saveUser(@RequestBody User user) {
        int result = userService.saveUser(user);
        return ResponseEntity.ok(result);
    }

    @PutMapping("updateUser")
    public ResponseEntity<Integer> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("deleteUser/{userId}")
    public ResponseEntity<String> deleteUser(@PathVariable("userId") int userId) {
        userService.deleteUser(userId);
        return ResponseEntity.ok("ok");
    }
}
